package cn.com.xy.sms.sdk.service.updateservice;

import android.annotation.SuppressLint;
import android.app.job.JobParameters;
import android.app.job.JobService;
import cn.com.xy.sms.sdk.constant.Constant;
import cn.com.xy.sms.sdk.db.entity.SysParamEntityManager;
import cn.com.xy.sms.sdk.log.LogManager;
import cn.com.xy.sms.sdk.net.NetUtil;
import cn.com.xy.sms.sdk.threadpool.SmartSmsThreadPoolManager;
import cn.com.xy.sms.util.ParseManager;

@SuppressLint("NewApi")
public class MonitorWifiService extends JobService {

    @Override
    public void onCreate() {
        super.onCreate();
        LogManager.i("xiaoyuan", "MonitorWifiService created");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogManager.i("xiaoyuan", "MonitorWifiService destroyed");
    }

    @Override
    public boolean onStartJob(JobParameters params) {
        LogManager.d("xiaoyuan", "MonitorWifiService onStartJob");       
        /* SDK-771 songzhirong 20170427 start*/
        SmartSmsThreadPoolManager.iccidPool.execute(new Runnable() {
            @Override
            public void run() {
            	try {
            		 if (NetUtil.checkAccessNetWork(1)) {
                         checkJarsUpdateWifiConnect();
                     }
				} catch (Throwable e) {
					LogManager.e("xiaoyuan", "onStartJob", e);
				}
            	 
            }
        });      
        /* SDK-771 songzhirong 20170427 end*/
        return false;
    }

    @Override
    public boolean onStopJob(JobParameters params) {
        LogManager.d("xiaoyuan", "MonitorWifiService onStopJob");
        return false;
    }

    public static void checkJarsUpdateWifiConnect() {
        long lastUpateTime = SysParamEntityManager.getLongParam("JARS_UPDATE_TIME", 0, Constant.getContext());

        long cyc = SysParamEntityManager.getLongParam(Constant.CONFIG_WIFI_CONNET_DOWNLOAD_TIME, Constant.DAY * 7,
                Constant.getContext());

        LogManager.d("xiaoyuan",
                " checkJarsUpdateWifiConnect lastUpateTime=" + lastUpateTime + " cyc=" + cyc
                        + " System.currentTimeMillis()=" + System.currentTimeMillis()
                        + " (System.currentTimeMillis() - lastUpateTime > cyc)="
                        + (System.currentTimeMillis() - lastUpateTime > cyc));

        if (System.currentTimeMillis() - lastUpateTime > cyc) {
            /* SDK-771 songzhirong 20170427 start*/
            LogManager.d("xiaoyuan", " checkJarsUpdateWifiConnect 超过设定周期，触发更新");

            SysParamEntityManager.setParam("JARS_UPDATE_TIME", String.valueOf(System.currentTimeMillis()));
            ParseManager.updateNow();
            
            /* SDK-771 songzhirong 20170427 end*/

        
        } else {
            LogManager.d("xiaoyuan", " checkJarsUpdateWifiConnect 没超过设定周期，不触发更新");

        }
    }
}
